package com.si.controller.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.si.entity.OperationLog;
import com.si.service.OperationLogService;
import com.si.util.IpAddressUtil;
import com.si.util.UserOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.Banner;
import com.si.entity.Info;
import com.si.entity.InfoTypeClassA;
import com.si.service.BannerService;
import com.si.util.FileUpload;

/**
 * 轮播图管理
 * @author x1c
 *
 */
@Controller
@RequestMapping("banner/manage")
public class BannerManageController {

	@Autowired
	private BannerService bannerService;
	@Autowired
	private OperationLogService operationLogService;
	
	/**
	 *  进入到列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="list")
	@UserOperate(module = "轮播图",methods = "轮播图管理-进入轮播图列表")
	public String list(ModelMap modelMap,@ModelAttribute("message") String message){
		List<Banner> bannerList = bannerService.getAll();
		modelMap.put("bannerList", bannerList);
		modelMap.put("message", message);
		return "managePage/banner/list";
	}
	
	/**
	 * 进入到修改页面
	 * @param modelmap
	 * @return
	 */
	@UserOperate(module = "轮播图",methods = "轮播图管理-进入修改轮播图界面")
	@RequestMapping(value="initUpdate/{id}")
	public String update(
			ModelMap modelMap,
			@PathVariable(value="id") Integer id){
		Banner banner=(Banner)bannerService.getObjectById(id);
		modelMap.put("banner", banner);
		return "managePage/banner/update";
	}
	
	/**
	 * 进入到添加页面
	 * @return
	 */
	@RequestMapping(value="initAdd")
	@UserOperate(module = "轮播图",methods = "轮播图管理-进入添加轮播图界面")
	public String initAdd(ModelMap modelMap) {
		return "managePage/banner/update";
	}
	
	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value="save")
	public String save(HttpServletRequest request,
					   RedirectAttributes redirectAttributes,
					   ModelMap modelMap,
					   @RequestParam(value = "id") Integer id,
					   @RequestParam(value = "url")String url) {
		Banner banner=(Banner)bannerService.getObjectById(id);
		String image = FileUpload.picvideoupload("upload/banner", "fileInput", request);
		if (!org.apache.commons.lang3.StringUtils.isBlank(image)) {
			banner.setImage("upload/banner/" + image);
		}
		banner.setCreateDate(new Date());
		banner.setUrl(url);
		bannerService.saveOrUpdate(banner);
		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功");
		operationLog.setOperationdetails("修改轮播图-序号为"+banner.getId());
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("轮播图");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		redirectAttributes.addFlashAttribute("message", "轮播图信息保存成功");
		return "redirect:/banner/manage/list.html";
	}
	
	/**
	 * 根据id 删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="delete/{id}")
	public String delete(@PathVariable(value="id") Integer id,
											 HttpServletRequest request,RedirectAttributes redirectAttributes){
		Banner banner=(Banner)bannerService.getObjectById(id);
		bannerService.delete(banner);
		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功");
		operationLog.setOperationdetails("删除轮播图-序号为"+banner.getId());
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("轮播图");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		redirectAttributes.addFlashAttribute("message", "轮播图信息删除成功");
		return "redirect:/banner/manage/list.html";

	}
}
